package com.elsa.DataStatistics.utils;

import com.elsa.DataStatistics.mapper.MenuMapper;
import com.elsa.DataStatistics.mapper.MenuOrderMapper;
import com.elsa.DataStatistics.mapper.OrderMapper;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : CountTotalProfitUtil  //类名
 * @Author : elsa //作者
 */
@Component
@Slf4j
public class CalculateUtil {

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    @Autowired
    private MenuMapper menuMapper;

    public Double getOrderTotalProfitByOId(String oId) {
        log.info("订单总利润：" + (getOrderTotalIncomeByOId(oId) - getOrderTotalExpendByOId(oId)));
        return (getOrderTotalIncomeByOId(oId) - getOrderTotalExpendByOId(oId));
    }

    public Double getOrderTotalExpendByOId(String oId) {
        Double TotalExpend = 0.0;
        List<MenuOrder> menuOrderList;
        menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);
        for(int i = 0; i < menuOrderList.size(); i++) {
            int Mid = menuOrderList.get(i).getMId();
            Menu menu = menuMapper.selectById(Mid);
            TotalExpend += menu.getMOldPrice() * menuOrderList.get(i).getMoNum();
        }
        log.info("订单总支出：" + TotalExpend);
        return TotalExpend;
    }

    public Double getOrderTotalIncomeByOId(String oId) {
        Double TotalIncome = 0.0;
        List<MenuOrder> menuOrderList;
        menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);
        for(int i = 0; i < menuOrderList.size(); i++) {
            TotalIncome += menuOrderList.get(i).getMoPrice() * menuOrderList.get(i).getMoNum();
        }
        log.info("订单总收入：" + TotalIncome);
        return TotalIncome;
    }
}
